<?php
$touser = $touser->row();
?>
<?php
if(isset($error) && $error != null){
    ?>
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3><?php echo $this->lang->line('message')?></h3>
            </div>
            <div class="modal-body">
                <div class="col-md-12">
                    <div class="row">
                        <?php echo $error;?>
                    </div>
                </div>
                <div style="clear:both"></div>
            </div>
            <div class="modal-footer">
                <button type="button" data-dismiss="modal" class="btn btn-default">Close</button>
            </div>
        </div>
    </div>
<?php }else{?>

    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3><?php echo $this->lang->line('Offer your project to');?> - <?php echo $touser->user_name;?></h3>
            </div>
            <form action="" method="POST" id="frmInvite">
                <div class="modal-body">
                    <div class="col-md-12">
                        <div class="row">
                            <div class="control-group form-group">
                                <?php echo form_token();?>
                                <div class="controls col-md-12">
                                    <label><?php echo $projectDetail->project_name;?></label>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="control-group form-group">
                                <div class="controls col-md-12">
                                    <label><?php echo $this->lang->line("message")?></label>
                                    <textarea class="form-control" name="message" rows="5" placeholder=""></textarea>
                                    <?php echo form_error('message')?>
                                </div>
                            </div>
                        </div>
                        <p class="loading"></p>
                    </div>
                    <div style="clear:both"></div>
                </div>
                <div class="modal-footer">
                    <input type="hidden" name="user_id" value="<?php echo $userid?>">
                    <input type="hidden" name="project_id" value="<?php echo $projectDetail->id?>">
                    <button type="submit" name="inviteProgram" value="1" class="btn btn-primary">Submit</button>
                    <button type="button" data-dismiss="modal" class="btn btn-default">Close</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        $(document).ready(function(){
            $("#frmInvite").validate({
                errorElement: "span", // contain the error msg in a span tag
                errorClass: 'help-block',
                errorPlacement: function (error, element) { // render error placement for each input type
                    if (element.attr("type") == "radio" || element.attr("type") == "checkbox") { // for chosen elements, need to insert the error after the chosen container
                        error.insertAfter($(element).closest('.form-group').children('div').children().last());
                    } else if (element.attr("name") == "dd" || element.attr("name") == "mm" || element.attr("name") == "yyyy") {
                        error.insertAfter($(element).closest('.form-group').children('div'));
                    } else {
                        error.insertAfter(element);
                        // for other inputs, just perform default behavior
                    }
                },
                highlight: function (element) {
                    $(element).closest('.help-block').removeClass('valid');
                    // display OK icon
                    $(element).closest('.form-group').removeClass('has-success').addClass('has-error').find('.symbol').removeClass('ok').addClass('required');
                    // add the Bootstrap error class to the control group
                },
                unhighlight: function (element) { // revert the change done by hightlight
                    $(element).closest('.form-group').removeClass('has-error');
                    // set error class to the control group
                },

                rules:{
                    "project_id" : {
                        required : true
                    },
                    "message" : {
                        required : true
                    },
                },
                messages:{
                    "rate":{
                        required : "<?php echo $this->lang->line('project_id_valid')?>"
                    },
                    "message":{
                        required : "<?php echo $this->lang->line('message_valid')?>"
                    },
                },
                submitHandler: function (form) {
                    jQuery.ajax({
                        type: "POST",
                        data: $("#frmInvite").serialize(),
                        url: BASE_URL + 'index.php/userList/inviteOneProgrammer/' + <?php echo $touser->id?>+'/'+<?php echo $projectDetail->id?>,
                        beforeSend: function() {
                            $('.loading').css("text-align", "center").html('<img src="<?php echo image_url("loading.gif")?>"/>');
                        },
                        success: function(data) {
                            $('.loading').empty();

                            var res = $.parseJSON(data);
                            if(res.status == "COMPLETED"){
                                $('.loading').html("<?php echo $this->lang->line('review_success')?>");
                                setTimeout(5000);
                                //window.location.reload();
                                $("#ajax-modal").attr('data-success','success');
                                $("#ajax-modal2").modal('hide');
                                $(".list-dev-recommended").find("[data-id='" + <?php echo $touser->id?> +"']").addClass('Invited').removeClass('item-dev-recommend-opacity').css('cursor','no-drop');
                                $(".list-dev-recommended").find("[data-id='" + <?php echo $touser->id?> +"']").find('#btn-invite-one').text('Invited');
                                $(".list-dev-recommended").find("[data-id='" + <?php echo $touser->id?> +"'] > span").removeClass('glyphicon-remove').addClass('glyphicon-ok green-2');
                            }else if(res.status == "ERROR"){
                                $('.loading').html(res.message);
                            }
                        }
                    });

                    return false;
                }


            });

        });

    </script>
<?php } ?>